<template>
  <view>
    <!-- <image src="/static/icon_icbd-1.png" class="w100 h100vh absolute" mode=""></image> -->
    <view class="w100 rowscl rowsm relative">
      <uni-status-bar></uni-status-bar>
      <image src="/static/wd_gr.png" class="w-160 h-160 mt-100 mb-100" mode=""></image>
      <view class="w-690 h-636 br-40 rowscl rowsm pt-36" style="background-color: rgba(255, 255, 255, .28);">
        <view class="w-610 h-100 br-20 bgff pl-30 pr-30 mb-32">
          <picker mode="selector" :range="characterList" @change="handelCharacter">
            <view class="w100 rowsb rowsm h-100">
              <input type="text" placeholder="请选择" disabled placeholder-class="fs-28 col333" v-model="character">
              <image src="/static/icon_scc-1.png" class="w-28 h-28" mode=""></image>
            </view>
          </picker>
        </view>
        <view class="w-610 h-100 br-20 bgff rowsb rowsm pl-30 pr-22 mb-32">
          <input type="text" :placeholder="`请输入${character == '教师端' ? '教工':'学'}号`" placeholder-class="fs-28 col333" v-model="studentID">
        </view>
        <view class="w-610 h-100 br-20 bgff rows rowsm pl-30 pr-30 mb-100">
          <input type="text" password placeholder="请输入密码" placeholder-class="fs-28 col333" v-model="password">
        </view>
        <view class="w-610 h-100 rowsc rowsm colfff fw-b fs-32 br-50 mb-28" style="background: linear-gradient(to bottom,#4897FF,#4878FF);" @click="login()">登录</view>
      </view>
    </view>
  </view>
</template>

<script>
import tools from '@/common/tools.js'
export default {
  data() {
    return {
      isAgree: false,
      characterList: ['教师端', '学生端'],
      password: '',
      studentID: '',
      character: ''
    };
  },
  onLoad() {
    if(sessionStorage.getItem('token')){
      uni.switchTab({
        url:'/pages/index/index'
      })
    }
    // const casLoginUrl = "http://192.168.10.20:8087/affairs/casLogin?cyjType=h5";  // 本地
    // const casLoginUrl = "http://mdypj.lyvc.edu.cn/lyzy/affairs/casLogin?cyjType=h5";
    // 使用 URLSearchParams 解析查询参数
    // const params = new URLSearchParams(window.location.search);
    // // 获取 token 和 name 参数
    // const token = params.get('token');
    // const name = params.get('name');
    // if (name) {
    //   uni.setStorageSync('flag', 'student')
    // } else {
    //   uni.setStorageSync('flag', 'teacher')
    // }
    // if (token) {
    //   uni.setStorageSync('token', token);
    //   uni.switchTab({
    //     url: '/pages/index/index'
    //   });
    // } else {
    //   window.location.href = casLoginUrl;
    // }
  },
  methods: {
    // @click="$tools.goNext('/pages/login/agreement?type=1')"
    toPage(type) {
      uni.navigateTo({
        url: '/pages/login/agreement?type=' + type
      })
    },
    handelCharacter(val) {
      this.character = this.characterList[val.detail.value]
      console.log('///', this.character)
    },
    login() {
      if (!this.character) return this.$tools.showtt('请选择角色')
      if (!this.studentID) return this.$tools.showtt('请输入学号')
      if (!this.password) return this.$tools.showtt('请输入密码')
      if (this.character == '学生端') {
        uni.setStorageSync('flag', 'student')
      } else {
        uni.setStorageSync('flag', 'teacher')
      }
      const data = {
        loginType: this.character == '学生端' ? 2 : 1,
        number: this.studentID,
        password: this.password,
      }
      this.$tools.axiosFromToken('POST', 'login', JSON.stringify(data), '加载中').then(res => {
        if (res.code != 200) return this.$tools.showtt(res.msg)
        // uni.setStorageSync('token', res.msg)
        sessionStorage.setItem('token', res.msg);
        uni.switchTab({
          url: '/pages/index/index'
        })
      })
    }
  }
}
</script>

<style lang="scss">
page {
  background: rgba($color: #7bbff0, $alpha: 0.2);
}
</style>
